package viettravel.guiandevent.map;

import java.net.URL;
import org.holoeverywhere.app.ProgressDialog;
import org.json.JSONException;
import org.json.JSONObject;

import viettravel.DTO.JSONParser;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import viettravel.guiandevent.map.search.GGDataReader;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class CategoryDialog extends Activity {

	private static final String MAP_API_URL = "http://maps.google.com/maps";
	private static final String EXPORT_TYPE = "json";
	private static final String UTF_8 = "utf-8";
	private static final int NUM_OF_PLACE = 20;
	private String location;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// setTheme(R.style.Holo_Animation_Dialog);
		setContentView(R.layout.layout_categories);
		setTitle(getString(R.string.categoryTitle));
		Bundle extra = getIntent().getExtras();
		if (extra == null)
			finish();
		try {
			location = extra.getString(Constants.MY_LOCATION);

		} catch (Exception e) {
			// TODO: handle exception
		}

		ImageButton categoryATM = (ImageButton) findViewById(R.id.categoryATM);
		ImageButton categoryCafe = (ImageButton) findViewById(R.id.categoryCafe);
		ImageButton categoryGasStation = (ImageButton) findViewById(R.id.categoryGasStation);
		ImageButton categoryHospital = (ImageButton) findViewById(R.id.categoryHospital);
		ImageButton categoryHotel = (ImageButton) findViewById(R.id.categoryHotel);
		ImageButton categoryRestaurant = (ImageButton) findViewById(R.id.categoryRestaurant);

		categoryATM.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_atm));
		categoryATM.setOnClickListener(categoryATMOnClickListender);
		
		categoryCafe.setOnClickListener(categoryCafeOnClickListender);
		categoryCafe.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_coffee));
		
		categoryGasStation
				.setOnClickListener(categoryGasStationOnClickListender);
		categoryGasStation.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_gas_station));
		
		categoryHospital.setOnClickListener(categoryHospitalOnClickListender);
		categoryHospital.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_hospital));
		
		categoryHotel.setOnClickListener(categoryHotelOnClickListender);
		categoryHotel.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_hotel));
		
		categoryRestaurant
				.setOnClickListener(categoryRestaurantOnClickListender);
		categoryRestaurant.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_restaurant));
	}

	OnClickListener categoryATMOnClickListender = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.v(Constants.TAG, "ATM");
			new PlaceAround().execute("atm", location);
		}
	};

	OnClickListener categoryCafeOnClickListender = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.v(Constants.TAG, "Cafe");
			new PlaceAround().execute("cafe", location);
		}
	};

	OnClickListener categoryGasStationOnClickListender = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.v(Constants.TAG, "Gas");
			new PlaceAround().execute("Gas%20Station", location);
		}
	};

	OnClickListener categoryHospitalOnClickListender = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.v(Constants.TAG, "Hospital");
			new PlaceAround().execute("hospital", location);
		}
	};

	OnClickListener categoryHotelOnClickListender = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.v(Constants.TAG, "Hotel");
			new PlaceAround().execute("hotel", location);
		}
	};

	OnClickListener categoryRestaurantOnClickListender = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.v(Constants.TAG, "Restaurant");
			new PlaceAround().execute("restaurant", location);
		}
	};

	private URL createUrl(String place, String location) {
		try {
			URL mapUri = new URL(MAP_API_URL + "?q=" + place + "&sll="
					+ location + "&num=" + NUM_OF_PLACE + "&output="
					+ EXPORT_TYPE + "&hl=" + "vi" + "&oe=" + UTF_8);
			return mapUri;
		} catch (Exception e) {
			return null;
		}

	}

	public class PlaceAround extends AsyncTask<String, Integer, String> {
		private ProgressDialog mProgressDialog;

		@Override
		protected void onPreExecute() {
			mProgressDialog = new ProgressDialog(CategoryDialog.this);
			mProgressDialog.setMessage("Get direction");
			mProgressDialog.setCancelable(false);
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.show();
		};

		@Override
		protected String doInBackground(String... params) {
			URL url = createUrl(params[0], params[1]);
			String jsonString =JSONParser.getResponseFromUrl(url);
			if (jsonString.contains("while(1);")) {
				jsonString = jsonString.replace("while(1);", "");
			}
			JSONObject jsonObject;
			String marker;
			try {
				jsonObject = new JSONObject(jsonString);
				marker = jsonObject.getString(Constants.TAG_OVERLAYS);
				marker = marker.replace("{\"markers\":", "");
				marker = GGDataReader.urlDecode(marker);
				return marker;
			} catch (JSONException e) {
				return null;
			}

		}

		protected void onPostExecute(String result) {
			if (mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
				if (!result.equals(null)) {
					Intent intent = new Intent(getBaseContext(),
							LocationListActivity.class);
					intent.putExtra(Constants.LOCATIONLIST, result);
					intent.putExtra(Constants.LOCATION_KEY, 1);
					intent.putExtra(Constants.MY_LOCATION, location);
					startActivity(intent);
					finish();
				}
			}
		}

	}
}
